package com.dsw.demo.rabbitmq.config;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * TopicRabbitMQ 配置类
 *
 * @author keyu.wang
 * @date 2018/5/7 10:40
 */
@Configuration
public class TopicRabbitMQConfig {

    public static final String TOPICEXCHANGE = "topic.exchange";//消息交换机的名字
    public static final String TOPICQUEUEA = "topic.queue.a";//消息队列名称a
    public static final String TOPICQUEUEB = "topic.queue.b";//消息队列名称b
    public static final String TOPICROUTINGKEY = "topic.routing.key.";//消息队列routingkey


    /**
     * 配置消息交换机 TopicExchange:多关键字匹配
     *
     * @param
     * @author keyu.wang
     * @date 2018/5/7 16:38
     */
    @Bean(name = "TopicExchange")
    public TopicExchange topicExchange() {
        return new TopicExchange(TOPICEXCHANGE, true, false);
    }


    /**
     * 配置消息队列
     *
     * @author keyu.wang
     * @date 2018/5/7 16:38
     */
    @Bean(name = "TopicQueueA")
    public Queue queueA() {
        return new Queue(TOPICQUEUEA, true); //队列持久
    }

    /**
     * 配置消息队列
     *
     * @author keyu.wang
     * @date 2018/5/7 16:38
     */
    @Bean(name = "TopicQueueB")
    public Queue queueB() {
        return new Queue(TOPICQUEUEB, true); //队列持久
    }


    /**
     * 将消息队列A与交换机绑定
     *
     * @param topicExchange 交换机
     * @param topicQueueA   队列
     * @author keyu.wang
     * @date 2018/5/7 16:37
     */
    @Bean
    public Binding bindingQueueA2Exchange(@Qualifier("TopicExchange") TopicExchange topicExchange, @Qualifier("TopicQueueA") Queue topicQueueA) {
        return BindingBuilder.bind(topicQueueA).to(topicExchange).with(TopicRabbitMQConfig.TOPICROUTINGKEY);
    }


    /**
     * 将消息队列B与交换机绑定
     *
     * @param topicExchange 交换机
     * @param topicQueueB   队列
     * @author keyu.wang
     * @date 2018/5/7 16:37
     */
    @Bean
    public Binding bindingQueueB2Exchange(@Qualifier("TopicExchange") TopicExchange topicExchange, @Qualifier("TopicQueueB") Queue topicQueueB) {
        return BindingBuilder.bind(topicQueueB).to(topicExchange).with(TopicRabbitMQConfig.TOPICROUTINGKEY + "#");//*表示一个词,#表示零个或多个词
    }


}
